<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
  import {mapActions, mapState} from 'vuex'
export default {
  name: 'App',
  computed: {
    ...mapState({
      needRefreshInfo: state => state.needRefreshInfo,

      needRefreshOutFrame: state => state.needRefreshOutFrame,
      needRefreshInFrame: state => state.needRefreshInFrame,
      needRefreshOutCard: state => state.needRefreshOutCard,
      needRefreshMiddleCard: state => state.needRefreshMiddleCard,
      needRefreshInCard: state => state.needRefreshInCard,
      needRefreshSubManager: state => state.needRefreshSubManager,
      needRefreshSubAgent: state => state.needRefreshSubAgent,

      reqTokenFailed: state => state.reqTokenFailed,
      reqTokenExpire: state => state.reqTokenExpire,
    }),

  },
  watch: {
    needRefreshInfo(newValue){
      this.refreshUserInfoNeed()
    },
    reqTokenFailed(newValue){
      this.$dialog.toast({
        mes: '您没有操作权限！',
        timeout: 1500,
        icon: 'error',
        callback: () => {

        }
      });
    },
    reqTokenExpire(newValue){
      this.$dialog.toast({
        mes: 'token无效或过期，请重新登录！',
        timeout: 1500,
        icon: 'error',
        callback: () => {
          this.$store.state.logined = false
          localStorage.setItem('token', '')
          localStorage.setItem('userid', '')
          location.href = this.$store.state.locationHeader
        }
      });
    }
  },
  methods: {
    ...mapActions([
      'getUserInfo',
      'getBuySetList',
      'getFactoryList',
      'getMaterialUsed'
    ]),
    getUrlParam: function () {
      var params= {};
      var str=location.href;
      var num=str.indexOf("?");
      str=str.substr(num+1);

      var arr=str.split("&");
      for(var i=0;i < arr.length;i++){
        num=arr[i].indexOf("=");
        if(num>0){
          var  name = arr[i].substring(0,num);
          var  value = arr[i].substr(num+1);
          params[name] = value
        }
      }
      return params
    },
    refreshUserInfoNeed(){
      let paramsInfo = {action: 'get'}
      this.getUserInfo(paramsInfo)
      let paramsUsed = {
        action: 'getvipused'
      }
      this.getMaterialUsed(paramsUsed)
    }
  },
  mounted(){
    if (localStorage.getItem('token')){
      let paramsInfo = {action: 'get'}
      this.getUserInfo(paramsInfo)

      let paramsSet = {
        action: 'getlist',
        page: 1,
        pagesize: 100
      }
      this.getBuySetList(paramsSet)
      this.getFactoryList({})

      let paramsUsed = {
          action: 'getvipused'
      }
      this.getMaterialUsed(paramsUsed)
    }

    //扫码之后
    var params = this.getUrlParam()
    if (!params['code']){
        return
    }
    console.log(localStorage.getItem('codepath'))
    if(localStorage.getItem('codepath') == '/'){

    }else if (localStorage.getItem('codepath') == '/regist'){
      this.$router.push('/regist')
    }else if (localStorage.getItem('codepath') == '/manager/authorization'){
      this.$router.push('/manager/authorization')
    }else if (localStorage.getItem('codepath') == '/manager/authorization'){
      this.$router.push('/wxmanager/authorization')
    }

  }
}
</script>

<style>
html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  font-size: 14px;
}

#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  position: relative;
  width: 100%; height: 100%;
  background: #fff;
}

</style>
